/**
 * [OrderController description]
 */
OrderFilterController = function () {

    // 获取门店列表
    this.getShopList = function(param, prePageParam, resetFunc) {
        var serverUrl = App.serverUrl +　"/app/order/shopList.do";
        var sucFunc = function (result) {
            //console.info("获取门店列表返回:" + JSON.stringify(result));
            if (result && result.success) {
                // 服务器返回成功 
                orderFilterParam.inited = true;
                var cityListData = result.resultObject.pageData;
                if (cityListData && cityListData.length == 1) {
                    var city = cityListData[0];
                    var shops = city.shops;
                    if (shops && shops.length == 1) {
                        var shop = shops[0];
                        orderFilterParam.singleShop=true;
                        orderFilterParam.params.shopId=shop.shopId;
                        orderFilterParam.params.shopName=shop.shopName;
                    } else {
                        orderFilterParam.cache = cityListData;  
                        orderFilterParam.singleShop = false;
                    }                    
                } else if (!cityListData || cityListData.length == 0) {
                    DialogUtil.dialogWarn("未获取门店");
                    orderFilterParam.singleShop = false;
                } else {
                    orderFilterParam.cache = cityListData;
                    orderFilterParam.singleShop = false;
                }
                //数据渲染
                CommonUtil.render($("#shopDiv"), "shopTpl", {data: orderFilterParam});   
                //设置筛选项
                //设置门店
                if (prePageParam.params.shopId != '') {
                    orderFilterParam.params.shopId=prePageParam.params.shopId;
                    orderFilterParam.params.shopName=prePageParam.params.shopName;    
                    if (!orderFilterParam.singleShop) {
                        $('#orderFilterPage .page-body .store-name').html(orderFilterParam.params.shopName);    
                    }            
                } else {
                    //不是单门店的情况
                    if (!orderFilterParam.singleShop) {
                        orderFilterParam.params.shopId='';
                        orderFilterParam.params.shopName='';  
                        $('#orderFilterPage .page-body .store-name').html("全部门店"); 
                    }    
                }
                //设置订单状态
                orderFilterParam.params.orderStatus=prePageParam.params.orderStatus;
                $('#orderFilterPage .filter-item').each(function(){
                    var orderStatus = $(this).attr("status");
                    if (orderStatus && orderStatus == prePageParam.params.orderStatus) {
                        $(this).addClass("selected");
                        return;
                    } 
                });

                //设置订单来源
                orderFilterParam.params.orderSource=prePageParam.params.orderSource;
                $('#orderFilterPage .filter-item').each(function(){
                    var orderSource = $(this).attr("source");
                    if (orderSource && orderSource == prePageParam.params.orderSource) {
                        $(this).addClass("selected");
                        return;
                    } 
                });
                    

                //设置支付方式
                orderFilterParam.params.payType=prePageParam.params.payType;
                $('#orderFilterPage .filter-item').each(function(){
                    var payType = $(this).attr("type");
                    if (payType && payType == prePageParam.params.payType) {
                        $(this).addClass("selected");
                        return;
                    } 
                });                
            } else {
                DialogUtil.dialogWarn(result.msg);
            }            
        }

        CommonUtil.sendJsonpRequest({
            url: serverUrl,
            params: param,
            success: sucFunc
        });
    };  

    // 获取角色,门店信息
    this.getSessionUser = function(param, resetFunc) {
        var serverUrl = App.serverUrl +　"app/user/info.do"; 
        var self = this;
        var sucFunc = function (result) {
            if (result && result.success) {
                orderFilterParam.inited = true;
                orderFilterParam.accountRole=result.resultObject.role;
                debugger
                if(orderFilterParam.accountRole=='4'){//代表是店长登录
                    // 服务器返回成功 
                    var prePageParam = CommonUtil.takeOutParamObject();
                    orderFilterParam.selectedOption =  prePageParam.selectedOption
                    orderFilterParam.params.shopId=result.resultObject.yzShopId;
                    orderFilterParam.params.shopName = result.resultObject.shopName;
                    CommonUtil.render($("#shopDiv"), "shopTpl", {data: orderFilterParam});   
                    $('#orderFilterPage .page-body .store-name').html(orderFilterParam.params.shopName);
                     //设置订单状态
                orderFilterParam.params.orderStatus=prePageParam.params.orderStatus;
                $('#orderFilterPage .filter-item').each(function(){
                    var orderStatus = $(this).attr("status");
                    if (orderStatus && orderStatus == prePageParam.params.orderStatus) {
                        $(this).addClass("selected");
                        return;
                    } 
                });

                //设置订单来源
                orderFilterParam.params.orderSource=prePageParam.params.orderSource;
                $('#orderFilterPage .filter-item').each(function(){
                    var orderSource = $(this).attr("source");
                    if (orderSource && orderSource == prePageParam.params.orderSource) {
                        $(this).addClass("selected");
                        return;
                    } 
                });
                    

                //设置支付方式
                orderFilterParam.params.payType=prePageParam.params.payType;
                $('#orderFilterPage .filter-item').each(function(){
                    var payType = $(this).attr("type");
                    if (payType && payType == prePageParam.params.payType) {
                        $(this).addClass("selected");
                        return;
                    } 
                });                
                }else if(orderFilterParam.accountRole=='1'){
                    var prePageParam = CommonUtil.takeOutParamObject();
                    orderFilterParam.selectedOption =  prePageParam.selectedOption
                    //获取门店数据
                    self.getShopList({}, prePageParam);    
                }
            }
        };
        CommonUtil.sendJsonpRequest({
            url: serverUrl,
            params: param,
            success: sucFunc
        });
    };
};